package com.yanqu.road.server.cmd.activity.evefeast;

import com.yanqu.road.dao.impl.player.UserEveFeastDaoImpl;
import com.yanqu.road.entity.activity.evefeast.EveFeastItem;
import com.yanqu.road.entity.activity.evefeast.UserEveFeastData;
import com.yanqu.road.logic.pb.EveFeastPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.EveFeastProto;
import com.yanqu.road.pb.activity.SilkRoadProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.evefeast.EveFeastModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.sync.message.SyncResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Cmd(code = GameProtocol.S_EVEFEAST_WATCH_MAX_SCORE_ITEMS, desc = "查看玩家最高分阵容")
public class EveFeastWatchMaxScoreItemsCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        EveFeastProto.EveFeastWatchMaxScoreItemsReqMsg reqMsg = EveFeastProto.EveFeastWatchMaxScoreItemsReqMsg.parseFrom(packet.getBytes());

        if (reqMsg.getServerId() == GameServer.getInstance().getServerId()) {
            UserEveFeastData userData = null;
            GamePlayer targetPlayer = GamePlayerMgr.getOnlinePlayer(reqMsg.getUserId());
            if (targetPlayer == null) {
                userData = new UserEveFeastDaoImpl().getUserEveFeastData(reqMsg.getActivityId(), reqMsg.getUserId());
            } else {
                userData = targetPlayer.getModule(EveFeastModule.class).getUserData();
            }

            if (userData != null) {
                EveFeastProto.EveFeastWatchMaxScoreItemsRespMsg.Builder respMsg = EveFeastProto.EveFeastWatchMaxScoreItemsRespMsg.newBuilder();
                respMsg.setRet(0);
                respMsg.setUserId(reqMsg.getUserId());
                List<EveFeastItem> maxScoreItems = new ArrayList<>(userData.getMaxScoreItems());
                for (EveFeastItem item : maxScoreItems) {
                    respMsg.addItems(EveFeastPb.parseGameItemTempMsg(item));
                }
                player.sendPacket(ClientProtocol.U_EVEFEAST_WATCH_MAX_SCORE_ITEMS, respMsg);
            }else{
                ret = GameErrorCode.E_USER_NO_FOUND;
            }
        } else {
            EveFeastProto.EveFeastWatchMaxScoreItemsRespMsg.Builder respMsg = EveFeastProto.EveFeastWatchMaxScoreItemsRespMsg.newBuilder();
            CompletableFuture<SyncResponse> future = GamePlayerMgr.sendToGameServer(reqMsg.getServerId(), GameProtocol.S_EVEFEAST_WATCH_MAX_SCORE_ITEMS, reqMsg.getUserId(), reqMsg.toBuilder(), respMsg);
            future.thenAccept((SyncResponse syncResponse) -> {
                if (syncResponse.getErrorCode() == 0) {
                    player.sendPacket(ClientProtocol.U_EVEFEAST_WATCH_MAX_SCORE_ITEMS, respMsg);
                } else {
                    player.sendPacket(ClientProtocol.U_EVEFEAST_WATCH_MAX_SCORE_ITEMS, EveFeastProto.EveFeastWatchMaxScoreItemsRespMsg.newBuilder().setRet(syncResponse.getErrorCode()));
                }
            });

        }

    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret > 0) {
            player.sendPacket(ClientProtocol.U_EVEFEAST_WATCH_MAX_SCORE_ITEMS, EveFeastProto.EveFeastWatchMaxScoreItemsRespMsg.newBuilder().setRet(ret));
        }
    }
}
